﻿using System;
using System.Web.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Net.Configuration;
using Marbles.Core.Connectors;
using System.IO;
using Marbles.Core.Settings;
using System.Net;
using Marbles.Core.Authentication;

namespace Marbles.Web.Admin.Content
{
    public partial class Config : ContentBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {

            if (SettingsManager.Connection != null)
            {
                cDrpProvider.SelectedValue = SettingsManager.Connection.ProviderName;
                cTxtHost.Text = SettingsManager.Connection.Host;
                cTxtUsername.Text = SettingsManager.Connection.Username;
                cTxtPassword.Text = SettingsManager.Connection.Password;
                cTxtPassword.Attributes["value"] = cTxtPassword.Text;
                cTxtDatabase.Text = SettingsManager.Connection.Schema;
            }

            cChkWysiwyg.Checked = SettingsManager.EnableWysiwygEditing;
            cTxtRunTimeRecipient.Text = SettingsManager.RunTimeErrorMailAddress;
            cChkRunTimeError.Checked = SettingsManager.EnableRunTimeErrorMailing;
            cChkShowFullDebugInfoToClient.Checked = SettingsManager.ShowFullDebugInfoToClient;
            cChkDevMode.Checked = SettingsManager.DeveloperMode;
            cTxtResourceDirectoryRoot.Text = SettingsManager.ResourceDirectoryRoot;
            cDrpAssetCompression.SelectedValue = SettingsManager.AssetCompression;
            cDrpAuthenticationProvider.SelectedValue = SettingsManager.AuthenticationProvider;
            cDrpPageCompression.SelectedValue = SettingsManager.PageCompression;
            cDrpPageStatePersister.SelectedValue = SettingsManager.PageStatePersister;
            cTxtGoogleAPIKey.Text = SettingsManager.GoogleAPIKey;
            cTxtUserFormMailAddress.Text = SettingsManager.UserFormMailAddress;
            cTxtTemplateDir.Text = SettingsManager.TemplateFolder;
            cTxtModuleInstallationDirectory.Text = SettingsManager.ModuleInstallationDirectory;
        }

        protected void cBtnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Admin/Default.aspx", true);
        }

        protected void cBtnSave_Click(object sender, EventArgs e)
        {
            DataConnectionSettings c = new DataConnectionSettings();
            c.ProviderName = cDrpProvider.SelectedValue;
            c.Host = cTxtHost.Text;
            c.Username = cTxtUsername.Text;
            c.Password = cTxtPassword.Text;
            c.Schema = cTxtDatabase.Text;
            SettingsManager.Connection = c;

            SettingsManager.ModuleInstallationDirectory = cTxtModuleInstallationDirectory.Text;
            SettingsManager.EnableWysiwygEditing = cChkWysiwyg.Checked;
            SettingsManager.ResourceDirectoryRoot = cTxtResourceDirectoryRoot.Text;
            SettingsManager.AssetCompression = cDrpAssetCompression.SelectedValue;
            SettingsManager.AuthenticationProvider = cDrpAuthenticationProvider.SelectedValue;
            SettingsManager.PageCompression = cDrpPageCompression.SelectedValue;
            SettingsManager.PageStatePersister = cDrpPageStatePersister.SelectedValue;
            SettingsManager.UserFormMailAddress = cTxtUserFormMailAddress.Text;
            SettingsManager.RunTimeErrorMailAddress = cTxtRunTimeRecipient.Text.Trim();
            SettingsManager.EnableRunTimeErrorMailing = cChkRunTimeError.Checked;
            SettingsManager.ShowFullDebugInfoToClient = cChkShowFullDebugInfoToClient.Checked;
            SettingsManager.DeveloperMode = cChkDevMode.Checked;
            SettingsManager.TemplateFolder = cTxtTemplateDir.Text;

            Core.Content.Sites.SiteDefinition web = null;


            if (Core.Content.Sites.SiteDefinition.GetSiteCount() == 0)
            {
                web = Core.Content.Sites.SiteDefinition.CreateNew();
                web.SiteName = "Default";
                web.Host = Context.Request.Url.Host;
                web.Title = "Default Site";
                web.Insert();
            }
            else
            {
                web = Core.Content.Sites.SiteDefinition.GetCurrent();
            }


            Response.Redirect("/Admin/Default.aspx", true);
        }

    }
}